from fastapi import APIRouter
from typing import Union, Optional, List
from pydantic import BaseModel, Field, field_validator
from datetime import date

app3 = APIRouter()


class Addr(BaseModel):
    province: str
    city: str


class User(BaseModel):
    name: str = "root"
    age: int = Field(0, ge=0, le=100)
    birth: Union[None, date] = None
    friends: List[int] = []
    description: Optional[str] = None
    addr: Addr

    # 正则表达式验证
    @field_validator("name")
    def name_must_alpha(cls, name):
        assert name.isalpha(), "name must be alpha"
        return name


class Data(BaseModel):
    data: List[User]


@app3.post("/user")
async def data(user: User):
    print(user, type(user))
    print(user.name, user.birth)
    print(user.dict())
    return user


@app3.post("/data")
async def data(data: Data):
    return data
